package home.projects.misc;

import java.io.File;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.DOMBuilder;
import org.jdom.output.XMLOutputter;

public class MainTest {

   public static void main(String args[]) throws Exception {
      DocumentBuilder builder = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
      DOMBuilder domBuilder = new DOMBuilder();
      Document jdomDoc = domBuilder.build(builder.parse(new File("src/home/projects/misc/Test.xml")));
      Iterator iter = jdomDoc.getRootElement().getChildren("JOB").iterator();
      while(iter.hasNext()) {
         Element jobElem = (Element)iter.next();
         if(jobElem.getChild("JOBNAME").getValue().equals("Second job")) {
            jobElem.getChild("INPUTFOLDER").setText("MY-NEW-LOCATION");
         }
      }
      XMLOutputter outputter = new XMLOutputter();
      outputter.output(jdomDoc, System.out);
   }

   private static void doStandard() throws Exception {
      // DocumentBuilder builder = DocumentBuilderFactory.newInstance()
      // .newDocumentBuilder();
      // Document doc = builder.parse(new
      // File("src/home/projects/misc/Test.xml"));
      // NodeList list = doc.getElementsByTagName("JOB");
      //
      // if (list == null || list.getLength() == 0)
      // return;
      //
      // for (int i = 0; i < list.getLength(); ++i) {
      // Node node = list.item(i);
      // NodeList children = node.getChildNodes();
      // if (node.getNodeValue().equals("Second Job")) {
      // for (int j = 0; j < children.getLength(); ++j) {
      // Node childNode = children.item(j);
      // if (childNode.getNodeName().equals("INPUTFOLDER")) {
      // childNode.setNodeValue("MY FOLDER");
      // }
      // }
      // }
      // }
      // System.out.println(doc);
   }

}